/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

#if 0
static void OnGtkPanedMeasure( GtkWidget *widget , GtkOrientation orientation , int for_size , int *minimum , int *natural , int *minimum_baseline , int *natural_baseline , gpointer user_data )
{
	static unsigned char	is_first_time = 1 ;
	int			ctl_1_width = (int)(intptr_t)user_data ;

	if( is_first_time == 1 )
	{
		if( ctl_1_width < 0 )
			ctl_1_width = gtk_widget_get_width(widget) + ctl_1_width ;
		gtk_paned_set_position( GTK_PANED(widget) , ctl_1_width );

		is_first_time = 0 ;
	}

	return;
}
#endif

#if 0
static void OnGtkPanedRealize( GtkWidget *widget , gpointer user_data )
{
	static unsigned char	is_first_time = 1 ;
	int			ctl_1_width = (int)(intptr_t)user_data ;

	if( is_first_time == 1 )
	{
		if( ctl_1_width < 0 )
			ctl_1_width = gtk_widget_get_allocated_width(widget) + ctl_1_width ;
		gtk_paned_set_position( GTK_PANED(widget) , ctl_1_width );

		is_first_time = 0 ;
	}

	return;
}
#endif

int CreatePanedLayout( struct ZlangRuntime *rt , struct ZlangDirectProperty_guipanedlayout *guipanedlayout_direct_prop , int orientation_type , int ctl_1_width , struct ZlangDirectProperty_guiwidget_common *guiwidget_common_1 , struct ZlangDirectProperty_guiwidget_common *guiwidget_common_2 )
{
	struct ZlangDirectProperty_guiwidget_common	*guipanedlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guipanedlayout_direct_prop ;

	guipanedlayout_common->gtk_widget = gtk_paned_new( orientation_type ) ;

	if( guiwidget_common_1 )
		gtk_paned_set_start_child( GTK_PANED(guipanedlayout_common->gtk_widget) , GetControlParentGtkWidget(guiwidget_common_1) );
	if( guiwidget_common_2 )
		gtk_paned_set_end_child( GTK_PANED(guipanedlayout_common->gtk_widget) , GetControlParentGtkWidget(guiwidget_common_2) );

	gtk_paned_set_position( GTK_PANED(guipanedlayout_common->gtk_widget) , ctl_1_width );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guipanedlayout_Create_guiwindow;
int ZlangInvokeFunction_guipanedlayout_Create_guiwindow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guipanedlayout	*guipanedlayout_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guipanedlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guipanedlayout_direct_prop ;
	int						nret = 0 ;

	nret = CreatePanedLayout( rt , guipanedlayout_direct_prop , 0 , 0 , NULL , NULL ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guipanedlayout_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guipanedlayout_Create_guiwindow_int_int;
int ZlangInvokeFunction_guipanedlayout_Create_guiwindow_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						orientation_type ;
	int32_t						ctl_1_width ;
	struct ZlangDirectProperty_guipanedlayout	*guipanedlayout_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guipanedlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guipanedlayout_direct_prop ;
	int						nret = 0 ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & orientation_type );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & ctl_1_width );

	nret = CreatePanedLayout( rt , guipanedlayout_direct_prop , orientation_type , ctl_1_width , NULL , NULL ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guipanedlayout_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guipanedlayout_Create_guiwindow_int_object_object_int;
int ZlangInvokeFunction_guipanedlayout_Create_guiwindow_int_object_object_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*in4 = GetInputParameterInLocalObjectStack(rt,4) ;
	struct ZlangObject				*in5 = GetInputParameterInLocalObjectStack(rt,5) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						orientation_type ;
	int32_t						ctl_1_width ;
	struct ZlangDirectProperty_guiwidget_common	*guictl_1_common = GetObjectDirectProperty(in3) ;
	struct ZlangDirectProperty_guiwidget_common	*guictl_2_common = GetObjectDirectProperty(in4) ;
	struct ZlangDirectProperty_guipanedlayout	*guipanedlayout_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guipanedlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guipanedlayout_direct_prop ;
	int						nret = 0 ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & orientation_type );
	CallRuntimeFunction_int_GetIntValue( rt , in5 , & ctl_1_width );

	nret = CreatePanedLayout( rt , guipanedlayout_direct_prop , orientation_type , ctl_1_width , guictl_1_common , guictl_2_common ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guipanedlayout_common->guiwindow_obj , in1 )

	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( panedlayout )

ZLANG_INVOKE_FUNCTION_guilayout_all_common_functions( panedlayout )

ZlangInvokeFunction ZlangInvokeFunction_guipanedlayout_GetChildWithType_int_object;
int ZlangInvokeFunction_guipanedlayout_GetChildWithType_int_object( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guinotebooklayout	*guipanedlayout_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guipanedlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guipanedlayout_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t						no ;
	GtkWidget					*gtk_child_widget = NULL ;
	struct ZlangObject				*clone_guictl_obj = NULL ;
	struct ZlangDirectProperty_guilayout		*clone_guictl_obj_direct_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*clone_guictl_common = NULL ;
	int						nret = 0 ;

	CallRuntimeFunction_int_GetIntValue( rt , in1 , & no );

	if( no == 1 )
		gtk_child_widget = gtk_paned_get_start_child( GTK_PANED(guipanedlayout_common->gtk_widget) ) ;
	else if( no == 2 )
		gtk_child_widget = gtk_paned_get_end_child( GTK_PANED(guipanedlayout_common->gtk_widget) ) ;
	else
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INVOKE_METHOD_RETURN , EXCEPTION_MESSAGE_PARAMETER_INVALID );
	}
	if( gtk_child_widget == NULL )
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INVOKE_METHOD_RETURN , EXCEPTION_MESSAGE_GET_CHILD_WIDGET_FAILED );
	}

	clone_guictl_obj = CloneObjectInTmpStack( rt , NULL , in2 ) ;
	if( clone_guictl_obj == NULL )
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INVOKE_METHOD_RETURN , EXCEPTION_MESSAGE_CLONE_OBJECT_FAILED );
	}

	clone_guictl_obj_direct_prop = GetObjectDirectProperty(clone_guictl_obj) ;
	clone_guictl_common = (struct ZlangDirectProperty_guiwidget_common *)clone_guictl_obj_direct_prop ;
	clone_guictl_common->gtk_widget = gtk_child_widget ;
	clone_guictl_common->guiwindow_obj = guipanedlayout_common->guiwindow_obj ;

	nret = ReferObject( rt , out1 , clone_guictl_obj ) ;
	if( nret )
	{
		UnreferObject( rt , out1 );
		return ThrowErrorException( rt , ZLANG_ERROR_INVOKE_METHOD_RETURN , EXCEPTION_MESSAGE_REFER_OBJECT_FAILED );
	}

	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guipanedlayout;
void *ZlangCreateDirectProperty_guipanedlayout( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guipanedlayout	*guipanedlayout_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*guipanedlayout_common = NULL ;
	
	guipanedlayout_prop = (struct ZlangDirectProperty_guipanedlayout *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guipanedlayout) ) ;
	if( guipanedlayout_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guipanedlayout_prop , 0x00 , sizeof(struct ZlangDirectProperty_guipanedlayout) );
	
	guipanedlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guipanedlayout_prop ;
	guipanedlayout_common->gtk_widget_type_id = GTK_TYPE_PANED ;

	return guipanedlayout_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guipanedlayout;
void ZlangDestroyDirectProperty_guipanedlayout( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guipanedlayout	*guipanedlayout_direct_prop = GetObjectDirectProperty(obj) ;
	
	ZLFREE( guipanedlayout_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guipanedlayout;
void ZlangSummarizeDirectPropertySize_guipanedlayout( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guipanedlayout) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guipanedlayout =
	{
		ZLANG_OBJECT_guipanedlayout , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guipanedlayout , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guipanedlayout , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guipanedlayout , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guipanedlayout;
struct ZlangObject *ZlangImportObject_guipanedlayout( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guipanedlayout , & direct_funcs_guipanedlayout , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guipanedlayout_Create_guiwindow , ZLANG_OBJECT_guipanedlayout , "Create" , ZLANG_OBJECT_guiwindow,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guipanedlayout_Create_guiwindow_int_int , ZLANG_OBJECT_guipanedlayout , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guipanedlayout_Create_guiwindow_int_object_object_int , ZLANG_OBJECT_guipanedlayout , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_object,NULL , ZLANG_OBJECT_object,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( panedlayout )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guilayout_all_common_functions( panedlayout )
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guipanedlayout_GetChildWithType_int_object , ZLANG_OBJECT_object , "GetChildWithType" , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_object,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

